﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forget.HRBLL.Model;
using System.Data.SqlClient;
using Forget.Data;
using System.Data;
using Forget.HRBLL.View;

namespace Forget.HRBLL.Logical
{
    public class EmployeeLogical
    {
        /// <summary>
        /// 添加员工信息
        /// </summary>
        /// <param name="numberID"></param>
        /// <param name="sex"></param>
        /// <param name="birthday"></param>
        /// <param name="height"></param>
        /// <param name="weight"></param>
        /// <param name="party"></param>
        /// <param name="place"></param>
        /// <param name="inTime"></param>
        /// <param name="epType"></param>
        /// <param name="idCard"></param>
        /// <param name="address"></param>
        /// <param name="education"></param>
        /// <param name="professional"></param>
        /// <param name="nation"></param>
        /// <param name="postionNum"></param>
        /// <param name="marry"></param>
        /// <returns></returns>
        public static bool AddEmployeeInfo(string numberID, bool sex, DateTime birthday, float height, float weight, string party,
            string place, DateTime inTime, int epType, string idCard, string address, string education, string professional,
            string nation, int postionNum, bool marry,string universtity,int departNum)
        {
            if (!string.IsNullOrEmpty(numberID))
            {
                EmployeeInfo ei = new EmployeeInfo() { NumberID = numberID };
                ei.Sex = sex;
                if (birthday != DateTime.MinValue) ei.Birthday = birthday;
                if (height > 0) ei.Height = height; else ei.Height = 0;
                if (weight > 0) ei.Weight = weight; else ei.Weight = 0;
                if (!string.IsNullOrEmpty(party)) ei.Party = party; else ei.Party = "";
                if (!string.IsNullOrEmpty(place)) ei.Place = place; else ei.Place = "";
                ei.InTime = inTime;
                ei.EpType = epType;
                ei.IDCard = idCard;
                if (!string.IsNullOrEmpty(address)) ei.Address = address; else ei.Address = "";
                if (!string.IsNullOrEmpty(education)) ei.Education = education; else ei.Education = "";
                if (!string.IsNullOrEmpty(professional)) ei.Profession = professional; else ei.Profession = "";
                if (!string.IsNullOrEmpty(nation)) ei.Nation = nation; else ei.Nation = "";
                if (!string.IsNullOrEmpty(universtity)) ei.University = universtity; else ei.University = "";
                ei.PositionNum = postionNum;
                ei.Marry = marry;
                ei.DpartNum = departNum;
                return EmployeeInfo.AddEmployeeInfo(ei);

            }
            return false;
        }

        /// <summary>
        /// 修改员工信息
        /// </summary>
        /// <param name="numberID"></param>
        /// <param name="sex"></param>
        /// <param name="birthday"></param>
        /// <param name="height"></param>
        /// <param name="weight"></param>
        /// <param name="party"></param>
        /// <param name="place"></param>
        /// <param name="inTime"></param>
        /// <param name="epType"></param>
        /// <param name="idCard"></param>
        /// <param name="address"></param>
        /// <param name="education"></param>
        /// <param name="professional"></param>
        /// <param name="nation"></param>
        /// <param name="postionNum"></param>
        /// <param name="marry"></param>
        /// <returns></returns>
        public static bool ModifyEmployeeInfo(string numberID, bool sex, DateTime birthday, float height, float weight, string party,
            string place, DateTime inTime, int epType, string idCard, string address, string education, string professional,
            string nation, int postionNum, bool marry,string university,int departNum)
        {
            if (!string.IsNullOrEmpty(numberID))
            {
                EmployeeInfo ei = new EmployeeInfo() { NumberID = numberID };
                ei.Sex = sex;
                if (birthday != DateTime.MinValue) ei.Birthday = birthday;
                if (height > 0) ei.Height = height; else ei.Height = 0;
                if (weight > 0) ei.Weight = weight; else ei.Weight = 0;
                if (!string.IsNullOrEmpty(party)) ei.Party = party; else ei.Party = "";
                if (!string.IsNullOrEmpty(place)) ei.Place = place; else ei.Place = "";
                ei.InTime = inTime;
                ei.EpType = epType;
                ei.IDCard = idCard;
                if (!string.IsNullOrEmpty(address)) ei.Address = address; else ei.Address = "";
                if (!string.IsNullOrEmpty(education)) ei.Education = education; else ei.Education = "";
                if (!string.IsNullOrEmpty(professional)) ei.Profession = professional; else ei.Profession = "";
                if (!string.IsNullOrEmpty(nation)) ei.Nation = nation; else ei.Nation = "";
                if (!string.IsNullOrEmpty(university)) ei.University = university; else ei.University = "";
                ei.PositionNum = postionNum;
                ei.Marry = marry;
                ei.DpartNum = departNum;
                return EmployeeInfo.EditEmployeeInfo(ei);
            }
            return false;
        }

        /// <summary>
        /// 获取员工信息
        /// </summary>
        /// <param name="numberID">员工编号</param>
        /// <returns></returns>
        public static EmployeeInfo GetInfo_NumberID(string numberID)
        {
            if (!string.IsNullOrEmpty(numberID))
            {
                EmployeeInfo eInfo = new EmployeeInfo() { NumberID = numberID };
                eInfo.Birthday = DateTime.MinValue;
                eInfo.InTime = DateTime.MinValue;
                return EmployeeInfo.SelectInfo_NumberID(eInfo);
            }
            return null;
        }

        public static bool Modify_Salary(List<EmployeeSalary> eSalaries)
        {
            if (eSalaries != null)
                return EmployeeInfo.ModifEmployeeSalary(eSalaries);
            return false;
        }
    }
}

namespace Forget.HRBLL.Model
{
    public partial class EmployeeInfo
    {
        private static readonly string SELECT_NUMBERID = "HR_SelectEmployeeInfo_NumberID";
        private static readonly string MODIFY_SALARY = "HR_ModifySalary";

        public static EmployeeInfo SelectInfo_NumberID(EmployeeInfo eInfo)
        {
            List<SqlParameter> parms = Convert<SqlParameter>.ObjectToParms(eInfo);
            List<EmployeeInfo> eInfos = null;
            try
            {
                DataTable table = DataAccess.ExecuteDataTable(CommandType.StoredProcedure, SELECT_NUMBERID, parms.ToArray());
                eInfos = Convert<EmployeeInfo>.DataConvertToObject(table);
            }
            catch (Exception ex)
            {
                Log.WriteLog("BU.SelEmpInfo_NumID", ex.Message, ex.StackTrace);
            }
            if (eInfos != null && eInfos.Count > 0)
                return eInfos[0];
            return null;
        }

        public static bool ModifEmployeeSalary(List<EmployeeSalary> eSalaries)
        {
            SqlTransaction trans = DataAccess.GetTransaction();
            try
            {
                foreach (EmployeeSalary eSalary in eSalaries)
                {
                    List<SqlParameter> parms = Convert<SqlParameter>.ObjectToParms(eSalary);
                    DataAccess.ExecuteNonQuery(trans, CommandType.StoredProcedure, MODIFY_SALARY, parms.ToArray());
                }
                trans.Commit();
            }
            catch (Exception ex)
            {
                trans.Rollback();
                Log.WriteLog("BU.ModifySalary", ex.Message, ex.StackTrace);
                return false;
            }
            return true;
        }
    }
}
